<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class Comment extends Model{
    /**列表
    * @tabledir 模块表
    * @signid 1通过2驳回3待审9已删除，大于9就是删除时间戳
    * @lang 语言
    * @keyword 关键词
    * @order 排序
    */
	static function index($tabledir,$signid,$lang,$keyword,$order){
		// 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline($tabledir);
        
		if($signid > 8){//已软删除
			$where[] = ['comment.signid','>','8'];
		}else{
			$where[] = ['comment.signid','=',$signid];
			// $where[] = ['comment.parentid','=',0];
		}
		$where[] = ['comment.lang','=',$lang];
		if(!empty($tabledir)) $where[] = ['comment.tabledir','=',$tabledir];

		$pageParam = array('lang'=>$lang);
        $pageParam += array('tabledir'=>$tabledir);
        $pageParam += array('signid'=>$signid);
        $pageParam += array('keyword'=>$keyword);

		if(!empty($tabledir)){
			$rs = Db::view('comment','*')
                ->view($tabledir,['title'=>'m_title'],"$tabledir.id=comment.tableid")
                ->view('user',['avatar','nickname'],"$tabledir.userid=user.id")
                ->where($where)->where("comment.content|$tabledir.title|user.nickname",'like',"%$keyword%")->order($order)
                ->paginate([
	            	'list_rows'=> 20,
	            	'query'=> $pageParam,
	            ]);
		}else{
			$rs = Db::view('comment','*')
                ->view('user',['avatar','nickname'],"comment.userid=user.id")
                ->where($where)->where("comment.content|user.nickname",'like',"%$keyword%")->order($order)
                ->paginate([
	            	'list_rows'=> 20,
	            	'query'=> $pageParam,
	            ]);
		}
		return $rs;
	}
	
	//日期
    protected function setAddtimeAttr(){
	    $addtime=strtotime(input('addtime'));
		return $addtime;
    }
    
    //新增编辑
	static function edit($id){
	    if($id>0){//编辑
			$comment = Comment::find($id);
		}else{//新增
			$comment = new Comment;
		}
		$comment->save(input('post.'));
		return $comment->id;
	}
}